package com.ruoyi.web.controller.outer;

import com.ruoyi.activiti.domain.dto.ActWorkflowFormDataDTO;
import com.ruoyi.activiti.service.IActTaskService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginBody;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.leave.domain.WorkflowLeave;
import com.ruoyi.leave.service.IWorkflowLeaveService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;

/**
 * 请假Controller
 *
 * @author danny
 * @date 2020-10-28
 */
@RestController
@RequestMapping("/outSystemUser")
public class OutSystemUserController extends BaseController {
    @Autowired
    private IWorkflowLeaveService workflowLeaveService;
    @Autowired
    private IActTaskService actTaskService;

    /**
     * 查询请假列表
     */

//    @PreAuthorize("@ss.hasPermi('workflow:leave:list')")
    @Autowired
    private SysLoginService loginService;
    @PostMapping("/login")
    @ApiOperation("外部登录")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.outLogin(loginBody.getUsername(), loginBody.getPassword());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    @PreAuthorize("@ss.hasPermi('workflow:leave:add')")
    @Log(title = "请假", businessType = BusinessType.INSERT)
    @PostMapping("/createFlow")
    @ApiOperation("发起流程")
    public AjaxResult createFlow(@RequestBody WorkflowLeave workflowLeave) {
        return toAjax(workflowLeaveService.createFlow(workflowLeave));
    }
    @PostMapping("/formDataSave")
    @ApiOperation("外部任务执行")
    public AjaxResult formDataSave(@PathVariable("taskID") String taskID,
                                   @RequestBody   List<ActWorkflowFormDataDTO> formData ) throws ParseException {
        return toAjax(actTaskService.formDataSave(taskID, formData));

    }

}
